﻿namespace LinesOfAction.Controls
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Shapes;
    using Enumerations;

    /// <summary>
    ///   Interaction logic for Checkerboard.xaml
    /// </summary>
    public partial class Checkerboard
    {
        private readonly List<Piece> BlackPieces = new List<Piece>(16);
        private readonly List<Piece> WhitePieces = new List<Piece>(16);

        public Checkerboard()
        {
            InitializeComponent();

            foreach (var i in Enumerable.Range(0, 16))
            {
                BlackPieces.Add(new Piece {Row = i/8, Column = i%8, Player = Players.Player1});
            }

            foreach (var i in Enumerable.Range(48, 16))
            {
                WhitePieces.Add(new Piece {Row = i/8, Column = i%8, Player = Players.Player2});
            }
        }


        private void DrawPieces()
        {
            foreach (var whitePiece in WhitePieces)
            {
                Children.Add(whitePiece);
                SetColumn(whitePiece, whitePiece.Column + 1);
                SetRow(whitePiece, whitePiece.Row + 1);
            }

            foreach (var blackPiece in BlackPieces)
            {
                Children.Add(blackPiece);
                SetColumn(blackPiece, blackPiece.Column + 1);
                SetRow(blackPiece, blackPiece.Row + 1);
            }
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            DrawAll();
        }

        private void DrawAll()
        {
            Children.Clear();
            DrawCheckerboard();
            DrawPieces();
        }

        private void DrawCheckerboard()
        {
            foreach (var column in Enumerable.Range(0, 9))
            {
                var rowDefinition = new RowDefinition {Height = new GridLength(1, GridUnitType.Star)};
                var columnDefinition = new ColumnDefinition {Width = new GridLength(1, GridUnitType.Star)};

                RowDefinitions.Add(rowDefinition);
                ColumnDefinitions.Add(columnDefinition);

                if (column != 0)
                {
                    foreach (var row in Enumerable.Range(1, 8))
                    {
                        var rec = new Rectangle();
                        SetColumn(rec, column);
                        SetRow(rec, row);
                        Children.Add(rec);
                    }
                }
                else
                {
                    foreach (var i in Enumerable.Range(1, 8))
                    {
                        var labelColuna = new Label
                            {
                                Content = (char) ('A' + i - 1),
                                HorizontalAlignment = HorizontalAlignment.Center,
                                VerticalAlignment = VerticalAlignment.Bottom,
                                Foreground = Brushes.AntiqueWhite,
                                FontSize = 16
                            };
                        var labelLinha = new Label
                            {
                                Content = i,
                                HorizontalAlignment = HorizontalAlignment.Right,
                                VerticalAlignment = VerticalAlignment.Center,
                                Foreground = Brushes.AntiqueWhite,
                                FontSize = 16
                            };
                        SetColumn(labelColuna, i);
                        SetRow(labelLinha, i);
                        Children.Add(labelLinha);
                        Children.Add(labelColuna);
                    }
                }
            }
        }
    }
}